﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CCWin;
using DC.Utils;
using Newtonsoft.Json;

namespace DC
{
    public partial class Form_Login : Skin_DevExpress
    {
        private Dictionary<string, User> users = new Dictionary<string, User>();
        public Form_Login()
        {
            InitializeComponent();
        }
        public bool success = false;
        public string UserID = string.Empty;
        Database DB = Global.GlobalDB;

        private void skinButton1_Click(object sender, EventArgs e)
        {
            if (skinComboBox1.Text == string.Empty || skinTextBox2.Text == string.Empty)
            {
                MessageBox.Show("请输入用户名或密码！", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            DataSet ds = DB.GetDs("select * from [User] where User_Name='" + skinComboBox1.Text + "' and passWord='" + skinTextBox2.Text + "'");
            if (ds.Tables[0].Rows.Count == 1)
            {
                success = true;
                UserID = ds.Tables[0].DefaultView[0]["User_ID"].ToString();

                //保存密码
                String userName = skinComboBox1.Text;
                String passWord = skinTextBox2.Text;
                User user = new User();
                FileStream fs = new FileStream(Application.StartupPath + "/config/userdata.bin", FileMode.OpenOrCreate);   //登录时，如果没有data.bin文件就创建，有就打开

                BinaryFormatter bf = new BinaryFormatter();
                user.UserName = userName;
                if (this.checkBox1.Checked)    //如果选择了记住密码功能，就在文件中保存密码
                {
                    user.PassWord = passWord;
                }
                else

                {
                    user.PassWord = "";
                }
                try
                {
                    if (users.ContainsKey(user.UserName))
                    {
                        users.Remove(user.UserName);
                    }
                    users.Add(user.UserName, user);   //添加用户信息到集合
                    bf.Serialize(fs, users);     //写入文件
                    fs.Close();
                }
                catch (Exception ep)
                {
                    MessageBox.Show(ep.Message);
                }
                this.Close();
            }
            else
            {
                MessageBox.Show("登录失败，用户名或密码错误！", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {

        }
        private void SaveData()
        {
            FileStream fs = new FileStream(Application.StartupPath + "/config/userdata.bin", FileMode.OpenOrCreate);  //读取文件流对象
            if (fs.Length > 0)
            {
                BinaryFormatter bf = new BinaryFormatter();
                users = bf.Deserialize(fs) as Dictionary<string, User>;   //读出存在userdata.bin里的用户信息
                foreach (User user in users.Values)     //循环添加
                {
                    this.skinComboBox1.Items.Add(user.UserName);
                }
                for (int i = 0; i < users.Count; i++)
                {
                    if (this.skinComboBox1.Text != "")
                    {
                        if (users.ContainsKey(this.skinComboBox1.Text))
                        {
                            this.skinTextBox2.Text = users[this.skinComboBox1.Text].PassWord;
                            this.checkBox1.Checked = true;
                        }
                    }
                }
            }
            fs.Close();
            if (this.skinComboBox1.Items.Count > 0)  //用户名默认选中第一个
            {
                this.skinComboBox1.SelectedIndex = this.skinComboBox1.Items.Count - 1;
            }
        }

        private void cmbUser_SelectedValueChanged(object sender, EventArgs e)   //点击ComboBox显示对应的密码
        {
            FileStream fs = new FileStream(Application.StartupPath + "/config/userdata.bin", FileMode.OpenOrCreate);   //首先读取记住密码的配置文件
            if (fs.Length > 0)
            {
                BinaryFormatter bf = new BinaryFormatter();
                users = bf.Deserialize(fs) as Dictionary<string, User>;
                for (int i = 0; i < users.Count; i++)
                {
                    if (this.skinComboBox1.Text != "")
                    {
                        if (users.ContainsKey(skinComboBox1.Text) && users[skinComboBox1.Text].PassWord != "")
                        {
                            this.skinTextBox2.Text = users[skinComboBox1.Text].PassWord;
                            this.checkBox1.Checked = true;
                        }
                        else
                        {
                            this.skinTextBox2.Text = "";
                            this.checkBox1.Checked = false;
                        }
                    }
                }
            }
            fs.Close();

        }
        private void Form_Login_Load(object sender, EventArgs e)
        {
            SaveData();
        }

        private void skinButton2_Click(object sender, EventArgs e)
        {
            Views.Form_NewUser newUser = new Views.Form_NewUser();
            newUser.ShowDialog();
        }

        private void skinButton6_Click(object sender, EventArgs e)
        {
            Views.Form_Settings form_Settings = new Views.Form_Settings();
            form_Settings.isfirst = false;
            form_Settings.ShowDialog();
        }
    }
}
